<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	
	/**
	 *
	 * @var Zend_Log
	 */
	protected $_logger;
	
	/**
	 *
	 * @var Zend_Application_Module_Autoloader
	 */
	protected $_resourceLoader;
	
	/**
	 *
	 * @var Zend_Controller_Front
	 */
	public $frontController;
	
// 	protected function _initViewHelpers() {
	// $this->bootstrap("layout");
	// $layout = $this->getResource('layout');
// 	$layout = new Zend_Layout();
// 	$layout->setViewBasePath(APPLICATION_PATH ."/layouts/");
// 	Zend_Registry :: set('layout', $layout);
	// var_dump($layout);exit;
// 	$view = $layout->getView();
// 	$view->doctype('XHTML1_STRICT');
// 	$view->headMeta()->appendHttpEquiv('Content-Type',
// 	'text/html;charset=utf-8');
// 	$view->headTitle()->setSeparator(' - ');
// 	$view->headTitle('Zend Framework Tutorial');
// 	}
	/**
	 * ini config
	 */
	
	protected function _initConfig() {
		$config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/config.ini', $this->getEnvironment () );
		
		Zend_Registry::set ( 'config', $config );
	}
	
	protected function _initLogging() {
		
		$logger = new Zend_Log ();
		
		$writer = new Zend_Log_Writer_Firebug ();
		$logger->addWriter ( $writer );
		
		if ('production' == $this->getEnvironment ()) {
			$filter = new Zend_Log_Filter_Priority ( Zend_Log::DEBUG );
			$logger->addFilter ( $filter );
		}
		
		$this->_logger = $logger;
		Zend_Registry::set ( 'log', $logger );
	}
	protected function _initDefaultController() {
		$config ['modules'] = Cff::getConfig ( 'modules' );
		$this->setOptions ( $config );
		$this->bootstrap ( 'frontController' );
		$frontController = $this->getResource ( 'frontController' );

// 		$frontController->setParam ( 'noViewRenderer', true );
	
	}
	protected function _initModuleAutoloader() {		
		$this->_logger->info ( 'Bootstrap ' . __METHOD__ );
		//法1
		$dir =  APPLICATION_PATH."/modules";
// 		echo $dir;
		$Ld = dir($dir);		
		while (false !== ($entry = $Ld->read())) {
			$checkdir=$dir."/".$entry;
			if (is_dir($checkdir)&&!preg_match("[^\.]",$entry)){
// 				var_dump($checkdir);
// 				var_dump($entry);
				$this->_resourceLoader = new Zend_Application_Module_Autoloader ( array ('namespace' => ucfirst ( $entry )."", 'basePath' => $checkdir ) );
				
			}
		}
		$Ld->close();
// 		exit;	
//法2	
// 		$modules = Cff::getConfig ( 'modules' );		
// 		foreach ( $modules as $key => $val ) {
// 			$this->_resourceLoader = new Zend_Application_Module_Autoloader ( array ('namespace' => ucfirst ( $key )."", 'basePath' => APPLICATION_PATH . $val ) );
// 		}

		
// 		var_dump($modules);exit;
// 		var_dump($this->_resourceLoader);exit;
	}

	
	/**
	 * ini layout
	 */
	public function _initLayout() {
		$layout = new Zend_Layout ();
		$layout->setLayoutPath ( APPLICATION_PATH . '/layouts' );
		// $layout->setViewSuffix("php");
		// $resource = $this->getPluginResource('layout');
		// var_dump($resource->getLayout());exit;
		Zend_Registry::set ( 'layout', $layout );
	}
	
	public function _initViews() {
		
		$config = Zend_Registry::get ( 'config' );
		$view = new Zend_View ();
		// $view->setScriptPath('./application/views/scripts/');//设置模板显示路径
		// $view->setBasePath();
		Zend_Registry::set ( 'CffView', $view );
	}
	
	public function _initSmartyViews() {
		$config = Zend_Registry::get ( 'config' );
// 		var_dump($config);exit;
		$view = new Cff_View_Smarty ( $config->smartyConfig->template_dir, $config->smartyConfig->config );
		
		Zend_Registry::set ( 'SmartyView', $view );
	}
	
	public function _initDba() {
		$resource = $this->getPluginResource ( 'db' );
		$db = $resource->getDbAdapter ();
		$config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/server.ini', $this->getEnvironment () );
		
		// var_dump($config->resources->db->params->prefix);exit;
		
		Zend_Registry::set ( "dbprefix", $config->resources->db->params->prefix );
		Zend_Db_Table_Abstract::setDefaultAdapter ( $db );
		Zend_Registry::set ( "db", $db );
	}
	
	// memcache 保存session
	public function _initSession() {
		$config = array ('memcached' => array (array ('ip' => '127.0.0.1', 'port' => 11211 ) ), 'lifetime' => 123334 );
		// create your Zend_Session_SaveHandler_Memcached
		// set the save handler for Zend_Session
		Zend_Session::setSaveHandler ( new Cff_Session_SaveHandler_Memcached ( $config ) );
		// start your session!
	}

	//初始化自定义动作助手
	protected function _initActionHelpers() {
		//用前缀形式 法1
		Zend_Controller_Action_HelperBroker::addPrefix('Cff_Controller_Action_Helpers');
		//指定目录和前缀 法2
		// 		Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/../libs/Cff/Controller/Action/Helpers',
		// 		                                   'Cff_Controller_Action_Helpers');
	
		//new一个助手类传入 法3
		// 		Zend_Controller_Action_HelperBroker::addHelper(new Cff_Controller_Action_Helpers_UrlParser());
	}
	//初始化自定义视图助手
	protected function _initViewHelpers() {
		$view = Zend_Registry::get ( 'CffView');
		$view->setHelperPath('Cff/View/Helpers/', 'Cff_View_Helpers'); //助手文件路径 ----同下
// 		$view->addHelperPath('Cff/View/Helpers/', 'Cff_View_Helpers'); //助手文件路径
// 		print_r($view->getHelperPaths()) ;exit;
	}
	//设定视图信息  基本用不到该设置 可以忽略不计  
	protected function _initViewRenders() {
		$view = Zend_Registry::get ( 'CffView');
// 		创建实例并注册自己的ViewRenderer对象，然后传入到助手经纪人。法1
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer->setView($view)->setViewSuffix('php');
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
	
	
// 	通过助手经纪人即时的初始化并/或获取ViewRenderer对象。 法2	
	
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$viewRenderer->setView($view)->setViewSuffix('php');
	}
	
	//控制器插件
	protected function _initControllerPlugin() {
		$front = Zend_Controller_Front::getInstance();
// 		$front->registerPlugin(new Cff_Controller_Plugins_Test());  
	}
	
	//加载自定义类
	protected function _initMyAutoload() {
		$myautoload = new Cff_Loader_Autoloader();
	}
	
	
	
	
}

/**
 * Cff Class static functions
 */

class Cff {
	/**
	 * getConfig
	 */
	
	public static function getConfig($key) {
		return Zend_Registry::get ( 'config' )->get ( $key );
	}
	
	/**
	 * rend smarty tpl
	 */
	public static function renderTpl($tpl, $layoutTemplate = null, $title = null, $scripts = null, $meta = null) {
		$layout = Zend_Registry::get ( 'layout' );
		
		if (null != $layoutTemplate && '' != $layoutTemplate) {
			$layout->setLayout ( $layoutTemplate );
		}
		if (is_array ( $scripts )) {
			foreach ( $scripts as $script ) {
				$layout->getView ()->headScript ()->appendFile ( self::getConfig ( 'baseUrl' ) . $script );
			}
		}
		
		$title = null == $title ? self::getConfig ( 'webTitle' ) : $title;
		$layout->getView ()->headTitle ( $title );
		
		if ($meta) {
			if (isset ( $meta ['keyword'] )) {
				$layout->getView ()->headMeta ()->appendName ( 'keywords', trim ( $meta ['keyword'] ) );
			}
			if (isset ( $meta ['description'] )) {
				$layout->getView ()->headMeta ()->appendName ( 'description', trim ( $meta ['description'] ) );
			}
		}
		
		$layout->content = Zend_Registry::get ( 'SmartyView' )->render ( $tpl );
		
		return $layout->render ();
	}
	
	public static function renderPhtml($tpl, $layoutTemplate = null, $title = null, $scripts = null, $meta = null) {
		$layout = Zend_Registry::get ( 'layout' );

		if (null != $layoutTemplate && '' != $layoutTemplate) {
			$layout->setLayout ( $layoutTemplate );
		}
		if (is_array ( $scripts )) {
			foreach ( $scripts as $script ) {
				$layout->getView ()->headScript ()->appendFile ( self::getConfig ( 'baseUrl' ) . $script );
			}
		}
		
		$title = null == $title ? self::getConfig ( 'webTitle' ) : $title;
		$layout->getView ()->headTitle ( $title );
		
		if ($meta) {
			if (isset ( $meta ['keyword'] )) {
				$layout->getView ()->headMeta ()->appendName ( 'keywords', trim ( $meta ['keyword'] ) );
			}
			if (isset ( $meta ['description'] )) {
				$layout->getView ()->headMeta ()->appendName ( 'description', trim ( $meta ['description'] ) );
			}
		}

		$layout->content = Zend_Registry::get ( 'CffView' )->render ( $tpl );
		
		return $layout->render ();
	}
}